<template>
  <el-row>
    <el-col :span="4"
      ><div class="title">
        <img
          @click="toHomepage"
          src="../assets/img/goods/标题1.png"
          alt=""
        /></div
    ></el-col>
    <el-col :span="14">
      <!-- 导航菜单 -->
      <el-menu mode="horizontal" text-color="rgb(73, 71, 71)" router>
        <el-menu-item index="/">首页</el-menu-item>
        <el-menu-item index="/items">二手</el-menu-item>
        <el-menu-item index="/demand">求购</el-menu-item>
        <el-sub-menu index="4">
          <template #title>更多</template>
          <el-menu-item index="2-1">没了</el-menu-item>
          <el-sub-menu index="2-2">
            <template #title>真的没了</template>
            <el-menu-item index="2-2-1">真的真的没了</el-menu-item>
          </el-sub-menu>
        </el-sub-menu>
      </el-menu>
    </el-col>
    <el-col :span="3"
      ><div class="publish">
        <!-- 发布菜单 -->
        <el-menu mode="horizontal" text-color="rgb(73, 71, 71)">
          <el-sub-menu menu-trigger="hover" index="4">
            <template #title>发布 </template>
            <div
              style="
                height: 150px;
                width: 250px;
                display: flex;
                justify-content: space-around;
              "
            >
              <div class="f1" style="position: relative; top: 25px">
                <img
                  @click="toNewGoods"
                  src="../assets/img/goods/icon_发布.png"
                  alt=""
                  style="width: 70px; cursor: pointer"
                />
                <span
                  style="
                    display: block;
                    margin-top: 10px;
                    text-align: center;
                    font-size: 15px;
                    color: rgb(94, 89, 89);
                  "
                  >发布商品</span
                >
              </div>
              <div class="f2" style="position: relative; top: 25px">
                <img
                  @click="toNewAsk"
                  src="../assets/img/goods/icon_求购.png"
                  alt=""
                  style="width: 70px; cursor: pointer"
                />
                <span
                  style="
                    display: block;
                    margin-top: 10px;
                    text-align: center;
                    font-size: 15px;
                    color: rgb(94, 89, 89);
                  "
                  >发布求购</span
                >
              </div>
            </div>
          </el-sub-menu>
        </el-menu>
      </div>
    </el-col>
    <el-col :span="3">
      <div class="login">
        <el-avatar
          :src="userInfo ? baseUrl + userInfo.profile.avatar : defaultImg"
          size="medium"
          @click="tousercenter"
        ></el-avatar>
        <div v-if="!isLogin" class="l1" @click="toLogin">
          <span>登录/注册</span>
        </div>
        <div v-if="isLogin" class="l2" @click="logout"><span>退出</span></div>
      </div>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from '../store'
import { User, getUserProfile } from '../services/auth'

const store = useStore()
const router = useRouter()
const userInfo = ref()
const baseUrl = 'http://159.75.101.15:3000/'
const defaultImg =
  'https://iconfont.alicdn.com/s/a1d1d4e6-4ff3-4b25-80fd-2720cc65b11f_origin.svg'

// 页面跳转
async function toHomepage() {
  await router.push('/')
}
async function toLogin() {
  await router.push('/auth/frame')
}
async function toNewGoods() {
  await router.push('/items/new')
}
async function toNewAsk() {
  await router.push('/demand/new')
}
async function tousercenter() {
  await router.push('/usercenter')
}

// 确认登录状态
const isLogin = ref(false)
onMounted(() => {
  const a = getUserProfile()
  a.then(
    (val) => {
      if (val) {
        userInfo.value = val.data
        isLogin.value = true
      }
    },
    (err) => {
      if (err) {
        isLogin.value = false
      }
    }
  )
})

// 登出
async function logout() {
  await store.dispatch('logout')
  await router.push('/items')
  location.reload()
}
</script>

<style lang="less" scoped>
.el-row {
  background-color: white;
}
.el-menu {
  padding: 0;
  border-bottom-width: 0;
  box-sizing: border-box;
}
.title img {
  width: 180px;
  position: relative;
  top: 3px;
  cursor: pointer;
}
.publish {
  margin-left: 40px;
}
.login {
  .el-avatar {
    margin-top: 12px;
  }
  .l1,
  .l2 {
    display: inline-block;
    position: relative;
    top: -12px;
    left: 20px;
    cursor: pointer;
    transition: all 0.35s;
  }
}
.login:hover {
  .l1 {
    color: rgb(0, 190, 238);
  }
  .l2 {
    color: rgb(233, 64, 64);
  }
}
</style>
